#include <stdio.h>
#include <string.h>
//using namespace std;

int main(int argc, const char *argv[])
{
	char buffer[100];
	char *a, *b, *c, *ret, *iter;

start:
	iter = buffer;
	puts ("Please enter a phone number, the format should be"
		  " (555)555-5555. " "(Input q to quit):");

	if (!fgets (iter, 100, stdin))
	{
		puts ("Error Reading from stdin.\n");
		return 1;
	}

	if (!strcmp (iter, "q\n")) 
		return 0;

	if (*iter == '(')
		iter++;
	else
		goto error;

	ret = strtok (iter, ")-");
	if (!ret)
		goto error;
	else
		a = ret;

	ret = strtok (NULL, ")-");
	if (!ret)
		goto error;
	else
		b = ret;

	ret = strtok (NULL, ")-");
	if (!ret)
		goto error;
	else
		c = ret;

	printf ("\nArea Code: %s\nPhone Number: %s%s\n\n", a, b, c);

	goto start;

error:
	puts ("Invalid Input, please retry.\n");
	goto start;

}
